package com.iwhalecloud.bss.kite.cucc.listener.handler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author wei.yanhong
 */
@Component
public class LocalProdInstFuncHandler {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    /**
     * 添加功能产品时，属性联动处理
     *
     * @param sceneInstId
     * @param funcProdInstId 功能产品实例ID
     * @param accessProdInstId 接入产品id
     */
    public void addProdInstFuncAttrDeal(String sceneInstId, String funcProdInstId, String funcProdId,
        String accessProdInstId, String custId) {

    }

    /**
     * 删除功能产品时，属性联动处理
     *
     * @param sceneInstId
     * @param funcProdInstId 功能产品实例ID
     * @param accessProdInstId 接入产品id
     */
    public void removeProdInstFuncAttrDeal(String sceneInstId, String funcProdInstId, String accessProdInstId,
        String custId) {
        KiteProdInstFunc prodInstFunc = querySceneInstService.getProdInstFunc(custId, funcProdInstId);
        if (Objects.nonNull(prodInstFunc)) {
            return;
        }
        if (LocalKeyConsts.FUNC_PROD_ID_APN.equals(prodInstFunc.getProdId()) || LocalKeyConsts.FUNC_PROD_ID_NBAPN
            .equals(prodInstFunc.getProdId()) || LocalKeyConsts.FUNC_PROD_ID_5GAPN.equals(prodInstFunc.getProdId()) || LocalKeyConsts.FUNC_PROD_ID_YF5GAPN.equals(prodInstFunc.getProdId())) {
            // 1、 如果是物联网产品，删除APN时，联动设置其他产品APN设置属性值
            // 获取APN 名称
            String apnName = this.getAPNNameByInst(prodInstFunc);

            dealApnOrgAttr(sceneInstId, accessProdInstId, custId, apnName);
        }

        dealLinkProdInstAttr(sceneInstId, accessProdInstId, custId, prodInstFunc);

    }

    private void dealLinkProdInstAttr(String sceneInstId, String accessProdInstId, String custId,
        KiteProdInstFunc prodInstFunc) {
        if (LocalKeyConsts.FUNC_PROD_ID_ZF.equals(prodInstFunc.getProdId()) || LocalKeyConsts.FUNC_PROD_ID_NBZF
            .equals(prodInstFunc.getProdId()) || LocalKeyConsts.FUNC_PROD_ID_5GZF.equals(prodInstFunc.getProdId()) || LocalKeyConsts.FUNC_PROD_ID_YF5GZF.equals(prodInstFunc.getProdId())) {
            KiteProdInst prodInst = querySceneInstService.getProdInst(custId, accessProdInstId);
            // 获取主产品实例的指定续约计划属性
            KiteAttr planAttr = prodInst.getAttr("200004027");

            if (Objects.nonNull(planAttr)) {
                String planAttrValue = planAttr.getValue();

                // 获取当前删除的资费计划的资费计划名称 200003698 ，如果当前属性值有选择，则需要移除掉
                String planName = prodInstFunc.getAttrValue("200003698");
                if (KiteStringUtils.isNotEmpty(planAttrValue) && planAttrValue.contains(planName)) {
                    StringBuffer newAttrValue = new StringBuffer("");
                    String[] values = planAttrValue.split(",");
                    for (String value : values) {
                        if (!KiteStringUtils.isEqual(value, planName)) {
                            newAttrValue.append(value + ",");
                        }
                    }
                    if (newAttrValue.toString().indexOf(",") > 0) {
                        newAttrValue.deleteCharAt(newAttrValue.length() - 1);
                    }

                    planAttr.setValue(newAttrValue.toString());
                    planAttr.setValueDesc(newAttrValue.toString());
                    operateSceneInstService
                        .setAttr(sceneInstId, prodInst.getInstType(), prodInst.getProdInstId(), planAttr);
                }
            }
        }
    }

    private void dealApnOrgAttr(String sceneInstId, String accessProdInstId, String custId, String apnName) {
        List<KiteProdInstFunc> txKiteProdInstFuncs = querySceneInstService
            .listProdInstFuncsByProdId(custId, accessProdInstId, LocalKeyConsts.FUNC_PROD_ID_TX);
        if (KiteListUtils.isEmpty(txKiteProdInstFuncs)) {
            txKiteProdInstFuncs = querySceneInstService
                .listProdInstFuncsByProdId(custId, accessProdInstId, LocalKeyConsts.FUNC_PROD_ID_NBTX);
        }
        if (KiteListUtils.isEmpty(txKiteProdInstFuncs)) {
            txKiteProdInstFuncs = querySceneInstService
                .listProdInstFuncsByProdId(custId, accessProdInstId, LocalKeyConsts.FUNC_PROD_ID_5GTX);
        }
        if (KiteListUtils.isEmpty(txKiteProdInstFuncs)) {
            txKiteProdInstFuncs = querySceneInstService
                .listProdInstFuncsByProdId(custId, accessProdInstId, LocalKeyConsts.FUNC_PROD_ID_YF5GTX);
        }
        if (KiteListUtils.isNotEmpty(txKiteProdInstFuncs)) {
            txKiteProdInstFuncs.forEach(funcInst -> {
                String attrValue = funcInst.getAttrValue("200004034");

                if (KiteStringUtils.isNotEmpty(attrValue) && attrValue.contains(apnName)) {
                    StringBuffer newAttrValue = new StringBuffer("");
                    String[] values = attrValue.split(",");
                    for (String value : values) {
                        if (!KiteStringUtils.isEqual(value, apnName)) {
                            newAttrValue.append(value + ",");
                        }
                    }
                    if (newAttrValue.toString().indexOf(",") > 0) {
                        newAttrValue.deleteCharAt(newAttrValue.length() - 1);
                    }
                    KiteAttr attr = funcInst.getAttr("200004034");
                    attr.setValue(newAttrValue.toString());
                    attr.setValueDesc(newAttrValue.toString());
                    operateSceneInstService
                        .setAttr(sceneInstId, funcInst.getInstType(), funcInst.getProdInstId(), attr);
                }
            });
        }

        List<KiteProdInstFunc> zfKiteProdInstFuncs = querySceneInstService
            .listProdInstFuncsByProdId(custId, accessProdInstId, LocalKeyConsts.FUNC_PROD_ID_ZF);
        if (KiteListUtils.isEmpty(zfKiteProdInstFuncs)) {
            zfKiteProdInstFuncs = querySceneInstService
                .listProdInstFuncsByProdId(custId, accessProdInstId, LocalKeyConsts.FUNC_PROD_ID_NBZF);
        }
        if (KiteListUtils.isEmpty(zfKiteProdInstFuncs)) {
            zfKiteProdInstFuncs = querySceneInstService
                .listProdInstFuncsByProdId(custId, accessProdInstId, LocalKeyConsts.FUNC_PROD_ID_5GZF);
        }
        if (KiteListUtils.isEmpty(zfKiteProdInstFuncs)) {
            zfKiteProdInstFuncs = querySceneInstService
                .listProdInstFuncsByProdId(custId, accessProdInstId, LocalKeyConsts.FUNC_PROD_ID_YF5GZF);
        }
        if (KiteListUtils.isNotEmpty(zfKiteProdInstFuncs)) {
            //                200004034 APN设置（通信计划）
            //                200004035 APN设置（资费计划）
            zfKiteProdInstFuncs.forEach(funcInst -> {
                String attrValue = funcInst.getAttrValue("200004035");

                if (KiteStringUtils.isNotEmpty(attrValue) && attrValue.contains(apnName)) {
                    StringBuffer newAttrValue = new StringBuffer("");
                    String[] values = attrValue.split(",");
                    for (String value : values) {
                        if (!KiteStringUtils.isEqual(value, apnName)) {
                            newAttrValue.append(value + ",");
                        }
                    }
                    if (newAttrValue.toString().indexOf(",") > 0) {
                        newAttrValue.deleteCharAt(newAttrValue.length() - 1);
                    }
                    KiteAttr attr = funcInst.getAttr("200004035");
                    attr.setValue(newAttrValue.toString());
                    attr.setValueDesc(newAttrValue.toString());
                    operateSceneInstService
                        .setAttr(sceneInstId, funcInst.getInstType(), funcInst.getProdInstId(), attr);
                }

            });
        }
    }

    private String getAPNNameByInst(KiteProdInstFunc funcProdInst) {
        // 共享、复用的apn展示的是apn域名，定制的展示的是apn的名称
        KiteAttr wayAttr = funcProdInst.getAttr("200003996");
        // 使用共享APN 100006950 200003997 共享APN域名
        // 使用已创建APN 100006951  200003998 复用APN域名
        // 我要定制 100006952 200004135 APN名称

        String apnName = "";
        if (Objects.nonNull(wayAttr)) {

            if ("100006952".equals(wayAttr.getValue())) {

                apnName = funcProdInst.getAttrValue("200004135");
            }
            else if ("100006950".equals(wayAttr.getValue())) {

                apnName = funcProdInst.getAttrValue("200003997");

            }
            else if ("100006951".equals(wayAttr.getValue())) {

                apnName = funcProdInst.getAttrValue("200003998");

            }
        }
        return apnName;
    }

}
